<template>
	<div class="app-container">
		<el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
			<el-form-item label="申请用户" prop="userNickName">
				<el-input v-model="queryParams.userNickName" placeholder="请输入申请用户" clearable @keyup.enter.native="handleQuery" />
			</el-form-item>
			<el-form-item label="邀请人" prop="inviterNickName">
				<el-input v-model="queryParams.inviterNickName" placeholder="请输入邀请人" clearable @keyup.enter.native="handleQuery" />
			</el-form-item>
			<el-form-item label="门店名称" prop="name">
				<el-input v-model="queryParams.name" placeholder="请输入名称" clearable @keyup.enter.native="handleQuery" />
			</el-form-item>
			<el-form-item label="手机号码" prop="tel">
				<el-input v-model="queryParams.tel" placeholder="请输入手机号码" clearable @keyup.enter.native="handleQuery" />
			</el-form-item>
			<el-form-item label="地址" prop="address">
				<el-input v-model="queryParams.address" placeholder="请输入地址" clearable @keyup.enter.native="handleQuery" />
			</el-form-item>
			<el-form-item label="类型" prop="type">
				<el-select v-model="queryParams.type" placeholder="请选择类型" clearable @change="handleQuery">
					<el-option label="回收门店" :value="1"></el-option>
					<el-option label="抵扣门店" :value="2"></el-option>
					<el-option label="消费补贴门店" :value="3"></el-option>
				</el-select>
			</el-form-item>
			<el-form-item>
				<el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
				<el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
			</el-form-item>
		</el-form>

		<el-row :gutter="10" class="mb8">
			<!-- <el-col :span="1.5">
				<el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['biz:storeApplyRecord:add']">新增</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate" v-hasPermi="['biz:storeApplyRecord:edit']">修改</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete" v-hasPermi="['biz:storeApplyRecord:remove']">删除</el-button>
			</el-col> -->
			<el-col :span="1.5">
				<el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport" v-hasPermi="['biz:storeApplyRecord:export']">导出</el-button>
			</el-col>
			<right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
		</el-row>

		<el-table v-loading="loading" :data="storeApplyRecordList" @selection-change="handleSelectionChange">
			<el-table-column type="selection" width="55" align="center" />
			<el-table-column label="主键" align="center" prop="id" />
			<el-table-column label="门店头像" align="center" prop="imgUrl">
				<template slot-scope="scope">
					<img :src="$store.state.app.imgUrl +scope.row.logoUrl" style="width:100px" alt="">
				</template>
			</el-table-column>
			<el-table-column label="营业执照" align="center" prop="imgUrl" width="280">
				<template slot-scope="scope">
					<div class="flex-column flex-alignCenter">
						<el-image style="width: 100px; height: 100px;" :src="$store.state.app.imgUrl +scope.row.storeBusinessLicense" :preview-src-list="[$store.state.app.imgUrl +scope.row.storeBusinessLicense]">
						</el-image>
						<div>公司名：{{scope.row.businessLicenseCompanyName}}</div>
						<div>注册号：{{scope.row.businessLicenseCreditCode}}</div>
						<div>法人：{{scope.row.businessLicenseLegalPerson}}</div>

					</div>
				</template>
			</el-table-column>
			<!-- <el-table-column label="类型" align="center" prop="typeName" /> -->
			<el-table-column label="分类" align="center" prop="categoryName" />
			<el-table-column label="申请用户" align="center" prop="userNickName" />
			<el-table-column label="申请用户角色" align="center" prop="userRoleName" />
			<el-table-column label="邀请人" align="center" prop="inviterNickName" />
			<el-table-column label="门店名称" align="center" prop="name" />
			<el-table-column label="手机号码" align="center" prop="tel" />
			<el-table-column label="地址" align="center" prop="address" />
			<el-table-column label="抵扣百分比" align="center" prop="deductionPercentage" />

			<!-- <el-table-column label="拒绝理由" align="center" prop="rejetctReason" /> -->
			<el-table-column label="备注" align="center" prop="remark" />
			<el-table-column label="创建时间" align="center" prop="createTime" />
			<el-table-column label="状态" align="center" prop="statusName" />
			<el-table-column label="操作" fixed="right" align="center" class-name="small-padding fixed-width">
				<template slot-scope="scope">
					<!-- <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" v-hasPermi="['biz:storeApplyRecord:edit']">修改</el-button> -->
					<el-button v-if="scope.row.status == 1" size="mini" type="text" icon="el-icon-edit" @click="deductionStoreApplyRecord(scope.row)" v-hasPermi="['biz:store:apply:record:approve']">审核</el-button>
					<!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['biz:storeApplyRecord:remove']"
          >删除</el-button> -->
				</template>
			</el-table-column>
		</el-table>

		<pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />

		<!-- 添加或修改biz_门店申请记录对话框 -->
		<el-dialog :title="title" :visible.sync="open" :close-on-click-modal='false' width="500px" append-to-body>
			<el-form ref="form" :model="form" :rules="rules" label-width="80px">
				<el-form-item label="分类id" prop="categoryId">
					<el-input v-model="form.categoryId" type="textarea" placeholder="请输入内容" />
				</el-form-item>
				<el-form-item label="用户id" prop="userId">
					<el-input v-model="form.userId" placeholder="请输入用户id" />
				</el-form-item>
				<el-form-item label="名称" prop="name">
					<el-input v-model="form.name" placeholder="请输入名称" />
				</el-form-item>
				<el-form-item label="手机号码" prop="tel">
					<el-input v-model="form.tel" placeholder="请输入手机号码" />
				</el-form-item>
				<el-form-item label="地址" prop="address">
					<el-input v-model="form.address" placeholder="请输入地址" />
				</el-form-item>
				<el-form-item label="纬度" prop="latitude">
					<el-input v-model="form.latitude" placeholder="请输入纬度" />
				</el-form-item>
				<el-form-item label="经度" prop="longitude">
					<el-input v-model="form.longitude" placeholder="请输入经度" />
				</el-form-item>
				<el-form-item label="营业时间" prop="openTime">
					<el-input v-model="form.openTime" placeholder="请输入营业时间" />
				</el-form-item>
				<el-form-item label="图片" prop="imgUrl">
					<el-input v-model="form.imgUrl" type="textarea" placeholder="请输入内容" />
				</el-form-item>
				<el-form-item label="logo" prop="logoUrl">
					<el-input v-model="form.logoUrl" placeholder="请输入logo" />
				</el-form-item>
				<el-form-item label="详情">
					<editor v-model="form.content" :min-height="192" />
				</el-form-item>
				<el-form-item label="拒绝理由" prop="rejetctReason">
					<el-input v-model="form.rejetctReason" placeholder="请输入拒绝理由" />
				</el-form-item>
				<el-form-item label="创建人id" prop="createById">
					<el-input v-model="form.createById" placeholder="请输入创建人id" />
				</el-form-item>
				<el-form-item label="更新人id" prop="updateById">
					<el-input v-model="form.updateById" placeholder="请输入更新人id" />
				</el-form-item>
				<el-form-item label="备注" prop="remark">
					<el-input v-model="form.remark" placeholder="请输入备注" />
				</el-form-item>
				<el-form-item label="抵扣百分比" prop="deductionPercentage">
					<el-input v-model="form.deductionPercentage" placeholder="请输入抵扣百分比" />
				</el-form-item>
				<el-form-item label="邀请码" prop="inviteCode">
					<el-input v-model="form.inviteCode" placeholder="请输入邀请码" />
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button type="primary" @click="submitForm">确 定</el-button>
				<el-button @click="cancel">取 消</el-button>
			</div>
		</el-dialog>
	</div>
</template>

<script>
import {
	deductionStoreApplyRecord,
	listStoreApplyRecord,
	getStoreApplyRecord,
	delStoreApplyRecord,
	addStoreApplyRecord,
	updateStoreApplyRecord,
} from '@/api/biz/storeApplyRecord';

export default {
	name: 'StoreApplyRecord',
	data() {
		return {
			// 遮罩层
			loading: true,
			// 选中数组
			ids: [],
			// 非单个禁用
			single: true,
			// 非多个禁用
			multiple: true,
			// 显示搜索条件
			showSearch: true,
			// 总条数
			total: 0,
			// biz_门店申请记录表格数据
			storeApplyRecordList: [],
			// 弹出层标题
			title: '',
			// 是否显示弹出层
			open: false,
			// 查询参数
			queryParams: {
				pageNum: 1,
				pageSize: 10,
				type: null,
				categoryId: null,
				userId: null,
				name: null,
				tel: null,
				address: null,
				latitude: null,
				longitude: null,
				openTime: null,
				imgUrl: null,
				logoUrl: null,
				content: null,
				status: null,
				rejetctReason: null,
				deductionPercentage: null,
				inviteCode: null,
			},
			// 表单参数
			form: {},
			// 表单校验
			rules: {},
		};
	},
	created() {
		this.getList();
	},
	methods: {
		/** 查询biz_门店申请记录列表 */
		getList() {
			this.loading = true;
			listStoreApplyRecord(this.queryParams).then((response) => {
				this.storeApplyRecordList = response.rows;
				this.total = response.total;
				this.loading = false;
			});
		},
		// 取消按钮
		cancel() {
			this.open = false;
			this.reset();
		},
		// 表单重置
		reset() {
			this.form = {
				id: null,
				type: null,
				categoryId: null,
				userId: null,
				name: null,
				tel: null,
				address: null,
				latitude: null,
				longitude: null,
				openTime: null,
				imgUrl: null,
				logoUrl: null,
				content: null,
				status: 0,
				rejetctReason: null,
				createByName: null,
				createById: null,
				createTime: null,
				updateByName: null,
				updateById: null,
				updateTime: null,
				deleted: null,
				remark: null,
				deductionPercentage: null,
				inviteCode: null,
			};
			this.resetForm('form');
		},
		/** 搜索按钮操作 */
		handleQuery() {
			this.queryParams.pageNum = 1;
			this.getList();
		},
		/** 重置按钮操作 */
		resetQuery() {
			this.resetForm('queryForm');
			this.handleQuery();
		},
		// 多选框选中数据
		handleSelectionChange(selection) {
			this.ids = selection.map((item) => item.id);
			this.single = selection.length !== 1;
			this.multiple = !selection.length;
		},
		/** 新增按钮操作 */
		handleAdd() {
			this.reset();
			this.open = true;
			this.title = '添加biz_门店申请记录';
		},
		/** 修改按钮操作 */
		handleUpdate(row) {
			this.reset();
			const id = row.id || this.ids;
			getStoreApplyRecord(id).then((response) => {
				this.form = response.data;
				this.open = true;
				this.title = '修改biz_门店申请记录';
			});
		},
		/** 提交按钮 */
		submitForm() {
			this.$refs['form'].validate((valid) => {
				if (valid) {
					if (this.form.id != null) {
						updateStoreApplyRecord(this.form).then((response) => {
							this.$modal.msgSuccess('修改成功');
							this.open = false;
							this.getList();
						});
					} else {
						addStoreApplyRecord(this.form).then((response) => {
							this.$modal.msgSuccess('新增成功');
							this.open = false;
							this.getList();
						});
					}
				}
			});
		},
		deductionStoreApplyRecord(row) {
			this.$confirm('请选择通过审核或者不通过', '审核', {
				distinguishCancelAndClose: true,
				confirmButtonText: '通过',
				cancelButtonText: '不通过',
			})
				.then(() => {
					const params = {
						id: row.id,
						status: 2,
					};
					deductionStoreApplyRecord(params).then((response) => {
						this.$message.success('操作成功');
						this.getList();
					});
				})
				.catch((action) => {
					if (action === 'cancel') {
						const params = {
							id: row.id,
							status: 3,
						};
						deductionStoreApplyRecord(params).then((response) => {
							this.$message.success('操作成功');
							this.getList();
						});
					}
				});
		},
		/** 删除按钮操作 */
		handleDelete(row) {
			const ids = row.id || this.ids;
			this.$modal
				.confirm(
					'是否确认删除biz_门店申请记录编号为"' + ids + '"的数据项？'
				)
				.then(function () {
					return delStoreApplyRecord(ids);
				})
				.then(() => {
					this.getList();
					this.$modal.msgSuccess('删除成功');
				})
				.catch(() => {});
		},
		/** 导出按钮操作 */
		handleExport() {
			this.download(
				'biz/storeApplyRecord/export',
				{
					...this.queryParams,
				},
				`storeApplyRecord_${new Date().getTime()}.xlsx`
			);
		},
	},
};
</script>
